package cn.firecode.data_collect.controller;


import cn.firecode.data_collect.domain.entity.CollectDrillT;
import cn.firecode.data_collect.service.CollectDrillTService;
import cn.firecode.data_collect.utils.DataResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangfan
 * @since 2019-12-10
 */
@RestController
@RequestMapping("/drill")
@Slf4j
public class CollectDrillTController {

    @Autowired
    private CollectDrillTService drillTService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public DataResponse add(Double length, Double diameter, String describe){
        QueryWrapper<CollectDrillT> wrapper = new QueryWrapper<>();
        wrapper.eq("length",length).eq("diameter",diameter).eq("is_delete",0);
        CollectDrillT one = drillTService.getOne(wrapper);
        if(one != null){
            log.info("该型号的钻头已经存在:"+one.toString());
            return DataResponse.failed(1,"该型号的钻头已经存在");
        }

        CollectDrillT drillT = new CollectDrillT();
        drillT.setCreateTime(LocalDate.now());
        drillT.setDescribeInfo(describe);
        drillT.setIsDelete(0);
        drillT.setLength(length);
        drillT.setDiameter(diameter);

        boolean saveResult = drillTService.save(drillT);
        if(saveResult){
            log.info("增加钻头信息成功:"+drillT.toString());
            return DataResponse.succeed("新增成功");
        }else{
            return DataResponse.failed(1, "新增失败");
        }

    }

    @RequestMapping(value = "/update/{drillId}" , method = RequestMethod.POST)
    public DataResponse update(@PathVariable("drillId") Integer drillId, Double length, Double diameter, String describe){
        CollectDrillT drillT = drillTService.getById(drillId);
        if(drillT == null){
            return DataResponse.failed(1,"该钻头信息不存在");
        }
        drillT.setDescribeInfo(describe);
        drillT.setLength(length);
        drillT.setDiameter(diameter);
        boolean result = drillTService.saveOrUpdate(drillT);
        if(result){
            log.info("修改钻头信息成功:"+drillT.toString());
            return DataResponse.succeed("修改成功");
        }else{
            return DataResponse.failed(1, "修改失败");
        }
    }

    @RequestMapping(value = "/delete/{drillId}" , method = RequestMethod.GET)
    public DataResponse delete(@PathVariable("drillId") Integer drillId){
        CollectDrillT drillT = drillTService.getById(drillId);
        drillT.setIsDelete(1);
        boolean result = drillTService.saveOrUpdate(drillT);
        if(result){
            log.info("删除钻头信息成功:"+drillT.toString());
            return DataResponse.succeed("删除成功");
        }else{
            return DataResponse.failed(1,"删除失败");
        }
    }

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public DataResponse getList(){
        QueryWrapper<CollectDrillT> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<CollectDrillT> list = drillTService.list(wrapper);
        return DataResponse.succeed(String.valueOf(list.size()),list);
    }

    @RequestMapping(value = "/get/{drillId}", method = RequestMethod.GET)
    public DataResponse getInfo(@PathVariable("drillId")Integer drillId){
        CollectDrillT byId = drillTService.getById(drillId);
        if(byId==null){
            return DataResponse.failed(1,"查询失败");
        }
        return DataResponse.succeed("查询成功",byId);

    }
}
